package com.ruoyi.standingbook.v2toilet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.standingbook.v2toilet.ToiletConst;
import com.ruoyi.standingbook.v2toilet.domain.V2ToiletBill;
import com.ruoyi.standingbook.v2toilet.domain.bo.V2ToiletBillBo;
import com.ruoyi.standingbook.v2toilet.domain.vo.V2ToiletBillVo;
import com.ruoyi.standingbook.v2toilet.bill.BillFlow;
import com.ruoyi.standingbook.v2toilet.bill.BillSendBean;
import com.ruoyi.standingbook.v2toilet.enums.ItemStatusEnum;
import com.ruoyi.standingbook.v2toilet.mapper.V2ToiletBillMapper;
import com.ruoyi.standingbook.v2toilet.service.IToiletBillService;
import com.ruoyi.standingbook.v2toilet.service.IToiletService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * V2市政公厕管养-异常工单Service业务层处理
 *
 * @author cc
 * @date 2023-06-07
 */
@RequiredArgsConstructor
@Service

public class V2ToiletBillServiceImpl implements IToiletBillService {

    private final V2ToiletBillMapper baseMapper;
    @SuppressWarnings("unused")
    private final IToiletService iToiletService;

    /**
     * 查询V2市政公厕管养-异常工单
     */
    @Override
    public V2ToiletBillVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }


    @Override
    public V2ToiletBillVo queryByFlowCode(ItemStatusEnum commit, String code) {
        V2ToiletBillBo bo = new V2ToiletBillBo();
        bo.setFlowCode(code);
        //        bo.setStatus(ToiletConst.BILL_COMMIT);
        bo.setStatus(commit.intCode);
        bo.setModified(ToiletConst.MODIFY_NOT);
        LambdaQueryWrapper<V2ToiletBill> lqw = Wrappers.lambdaQuery();
        lqw.eq(Objects.nonNull(bo.getFlowCode()), V2ToiletBill::getFlowCode, bo.getFlowCode());
        lqw.eq(Objects.nonNull(bo.getStatus()), V2ToiletBill::getStatus, bo.getStatus());
        lqw.eq(Objects.nonNull(bo.getModified()), V2ToiletBill::getModified, bo.getModified());
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 查询V2市政公厕管养-异常工单列表
     */
    @Override
    public TableDataInfo<V2ToiletBillVo> queryPageList(V2ToiletBillBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<V2ToiletBill> lqw = buildQueryWrapper(bo);
        Page<V2ToiletBillVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询V2市政公厕管养-异常工单列表
     */
    @Override
    public List<V2ToiletBillVo> queryList(V2ToiletBillBo bo) {
        LambdaQueryWrapper<V2ToiletBill> lqw = buildQueryWrapper(bo);
        lqw.eq(Objects.nonNull(bo.getFlowCode()), V2ToiletBill::getFlowCode, bo.getFlowCode());
        lqw.orderByAsc(V2ToiletBill::getStatus);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public V2ToiletBillVo queryOne(V2ToiletBillBo bo) {

        return baseMapper.queryOne(bo);
    }

    private LambdaQueryWrapper<V2ToiletBill> buildQueryWrapper(V2ToiletBillBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<V2ToiletBill> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getToiletId()), V2ToiletBill::getToiletId, bo.getToiletId());
        lqw.eq(StringUtils.isNotBlank(bo.getItem()), V2ToiletBill::getItem, bo.getItem());
        lqw.eq(Objects.nonNull(bo.getStatus()), V2ToiletBill::getStatus, bo.getStatus());
        lqw.orderByAsc(V2ToiletBill::getInspectDate);


        //    lqw.orderByDesc(V2ToiletImpBase::getCreateTime);
        return lqw;
    }

    /**
     * 新增V2市政公厕管养-异常工单
     */
    @Override
    public Boolean insertByBo(V2ToiletBillBo bo) {
        V2ToiletBill add = BeanUtil.toBean(bo, V2ToiletBill.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改V2市政公厕管养-异常工单
     */
    @Override
    public Boolean updateByBo(V2ToiletBillBo bo) {
        V2ToiletBill update = BeanUtil.toBean(bo, V2ToiletBill.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(V2ToiletBill entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除V2市政公厕管养-异常工单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        //        return baseMapper.deleteBatchIds(ids) > 0;
        return baseMapper.realDelByIds(ids) > 0;
    }

    @Override
    public Boolean batchInsertByBo(List<V2ToiletBillBo> bills) {
        List<V2ToiletBill> list = BeanCopyUtils.copyList(bills, V2ToiletBill.class);
        return baseMapper.insertBatch(list);
    }

    @Override
    public Boolean batchInsertOrUpdate(List<V2ToiletBillBo> bills) {
        List<V2ToiletBill> list = BeanCopyUtils.copyList(bills, V2ToiletBill.class);
        return baseMapper.insertOrUpdateBatch(list);
    }


    @Override

    public List<String> listChangeInfo(Map<String, Object> map) {
        return this.baseMapper.listChangeInfo(map);
    }

    @Override
    public List<BillSendBean> querySendValue(V2ToiletBillBo queryBo) {
        if (Objects.isNull(queryBo)) {
            queryBo = new V2ToiletBillBo(ItemStatusEnum.ERR);
        }
        if (Objects.isNull(queryBo.getStatus()))
            queryBo.setStatus(ItemStatusEnum.ERR.intCode);
        return this.baseMapper.querySendValue(queryBo);
    }

    @Override
    public int updateByFlowCode(ItemStatusEnum commit, List<String> ids) {
        Map<String, Object> query = new HashMap<>();
        query.put("status", commit.intCode);

        query.put("ids", ids);
        System.out.println(query.get("ids"));
        return this.baseMapper.updateByFlowCode(query);
    }

    @Override
    public V2ToiletBillVo flowData(V2ToiletBillBo bo) {

        List<V2ToiletBillVo> bills = this.queryList(bo);
        if (bills.isEmpty()) {//假的老数据
            V2ToiletBillVo empVo = new V2ToiletBillVo();
            Map<String, BillFlow> flowMap = new HashMap<>();
            flowMap.put("1", new BillFlow());
            flowMap.put("2", new BillFlow());
            flowMap.put("3", new BillFlow());
            empVo.setFlowData(JSONUtil.toJsonStr(flowMap));
            return empVo;
        }
        return bills.get(0);

    }


}
